aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/send/ark/[address]/+layout.server.ts
blob: 6381463aa2427ea020ef63815957d7021cbc4a0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { auth, get } from "$lib/utils";
import { redirect } from "@sveltejs/kit";

export async function load({ cookies, params, parent }) {
  const { user } = await parent();
  const aid = cookies.get("aid") || user.id;
  const { address, amount } = params;
  const { balance } = await get(`/account/${aid}`, auth(cookies));

  let invoice;
  try {
    invoice = await get(`/invoice/${address}`);
  } catch {
    // invoice not found
  }

  if (invoice) {
    const recipient = await get(`/users/${invoice.uid}`);
    let r = `/pay/${recipient.username}`;
    if (amount) r += `/${amount}`;
    if (recipient?.id !== user.id && recipient.id === invoice.aid) redirect(307, r);
  }

  return { balance };
}